package com.fiveGroup.util;

import cn.hutool.core.util.RandomUtil;
import com.fiveGroup.pojo.vo.DataVo;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

@Slf4j
public class EmailUtil {


    public static DataVo sendMail(String email, String username, JavaMailSender mailSender ) {
        //验证码生成4位数字符串hutool包
        String code = RandomUtil.randomString(6);

        //验证码标题
        String subject = "【思诚科技】验证码";

        //验证码的内容格式与具体内容
        String content = "您的验证码为:" + code + "有效期为5分钟,请勿泄露验证码";
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            //邮件发送人
            helper.setFrom(username);
            //邮件接收人
            helper.setTo(email);
            //设置邮件标题与内容
            helper.setSubject(subject);
            helper.setText(content);
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("验证码发送失败:{}", e.getMessage());
            return DataVo.fail("验证码发送失败");
        }
        return DataVo.success();
    }
}
